import router from "@/router";
import { Message } from "element-ui";

import NProgress from "nprogress";
import "nprogress/nprogress.css";

import { getToken } from "@/service/cookieFun";

// 未登录可进白名单
const whiteList = ["/", "/test"];

NProgress.configure({
  showSpinner: false
});

router.beforeEach((to, from, next) => {
  // 启动浏览器加载进度条
  NProgress.start();

  if (getToken()) {
    // 已登录
    next();
  } else {
    // 未登录
    if (whiteList.indexOf(to.path) !== -1) {
      // 白名单
      next();
    } else {
      // 重定向
      Message({
        message: `无法跳转到该页面：${to.fullPath}`,
        type: "warning",
        duration: 5 * 1000,
      });
      next(`/?redirect=${to.fullPath}`);
    }
  }
});

router.afterEach(() => {
  // 关闭浏览器加载进度条
  NProgress.done();
});
